import 'package:flutter/material.dart';

class Throttler {
  final Duration duration;
  DateTime? _lastRun;

  Throttler({required this.duration});

  void run(VoidCallback callback) {
    final lastRun = _lastRun;
    final now = DateTime.now();
    if (lastRun == null) {
      _lastRun = now;
      callback();
    } else {
      final diff = now.difference(lastRun);
      if (diff < duration) {
        return;
      }
      _lastRun = now;
      callback();
    }
  }
}

class ThrottlingButton extends StatelessWidget {
  final buttonThrottler = Throttler(duration: Duration(seconds: 1));

  ThrottlingButton({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("按钮点击节流"),
      ),
      body: SafeArea(
        child: TextButton(
          onPressed: () {
            buttonThrottler.run(() => print('button pressed'));
          },
          child: Text('Hello '),
        ),
      ),
    );
  }
}

const blockedWords = {'foo', 'bar', 'baz'};

extension Contains on List<String> {
  bool containsAny(Set<String> values) =>
      {...this}.lowered.intersection(values.lowered).isNotEmpty;
}

extension Lower on Set<String> {
  Set<String> get lowered => {...map((e) => e.toLowerCase())};
}
